package com.ld.hpfs.common.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

public class DoubleUtil {
	public DoubleUtil() {
	}

	public static Double getDouble(String doubleStr) {
		if (doubleStr == null || doubleStr.trim().equalsIgnoreCase("")) {
			doubleStr = "-1.0";
		}
		return Double.parseDouble(doubleStr);
	}

	public static boolean isSame(Double dou1, Double dou2) {
		if (dou1.doubleValue() == dou2.doubleValue()) {
			return true;
		} else {
			return false;
		}
	}

	/*
	 * 是否正数
	 */
	public static boolean isPositive(Double d) {
		return d > ZERO;
	}

	/*
	 * 是否负数
	 */
	public static boolean isNegative(Double d) {
		return d < ZERO;
	}

	/*
	 * 是否零
	 */
	public static boolean isZero(Double d) {
		return ZERO.equals(d);
	}

	/*
	 * 加运算
	 */
	public static double add(Double v1, Double... factors) {
		BigDecimal b1 = new BigDecimal(Double.toString(v1));

		for (Double v : factors) {
			BigDecimal b2 = new BigDecimal(Double.toString(v));
			b1 = b1.add(b2);
		}
		return b1.doubleValue();
	}

	/*
	 * 减运算
	 */
	public static double sub(Double v1, Double... factors) {
		BigDecimal b1 = new BigDecimal(Double.toString(v1));

		for (Double v : factors) {
			BigDecimal b2 = new BigDecimal(Double.toString(v));
			b1 = b1.subtract(b2);
		}
		return b1.doubleValue();
	}

	/*
	 * 乘运算
	 */
	public static double mul(Double v1, Double... factors) {
		BigDecimal b1 = new BigDecimal(Double.toString(v1));

		for (Double v : factors) {
			BigDecimal b2 = new BigDecimal(Double.toString(v));
			b1 = b1.multiply(b2);
		}
		return b1.doubleValue();
	}

	/*
	 * 除运算
	 */
	public static double div(Double v1, Double v2) {
		return div(v1, v2, DEFAULT_DIV_SCALE);
	}

	public static double div(Double v1, Double v2, int scale) {
		if (scale < 0) {
			throw new IllegalArgumentException(
					"the scale must be a positive integer or zero");
		}

		BigDecimal b1 = new BigDecimal(Double.toString(v1));
		BigDecimal b2 = new BigDecimal(Double.toString(v2));
		return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
	}

	/*
	 * 除运算
	 */
	public static double div2(Double v1, Double v2) { // ..ROUND_HALF_DOWN
		BigDecimal bd = new BigDecimal(v1);
		BigDecimal bd2 = new BigDecimal(v2);
		BigDecimal result = bd.divide(bd2, 6, BigDecimal.ROUND_HALF_DOWN);
		return result.doubleValue();
	}

	/*
	 * 返回Double整数部分位数
	 */
	public static int getIntegralLength(Double d) {
		if (d != null) {
			return String.valueOf(d.intValue()).length();
		} else {
			return 0;
		}
	}

	/*
	 * 返回Double小数部分位数
	 */
	public static int getDecimalLength(Double d) {
		if (d != null && String.valueOf(d).contains(".")) {
			return String.valueOf(d.doubleValue()).length()
					- (1 + String.valueOf(d.intValue()).length());
		} else {
			return 0;
		}
	}

	/*
	 * Format至指定的小数位数
	 */
	public static double formatDigitDecimal(Double original, int digit) {
		Double rtnDouble;
		switch (digit) {
		case zeroDigitDecimal:
			zeroDecimalFormat.setRoundingMode(RoundingMode.HALF_UP);
			rtnDouble = Double.parseDouble(zeroDecimalFormat.format(original));
			break;
		case oneDigitDecimal:
			oneDecimalFormat.setRoundingMode(RoundingMode.HALF_UP);
			rtnDouble = Double.parseDouble(oneDecimalFormat.format(original));
			break;
		case twoDigitDecimal:
			twoDecimalFormat.setRoundingMode(RoundingMode.HALF_UP);
			rtnDouble = Double.parseDouble(twoDecimalFormat.format(original));
			break;
		case threeDigitDecimal:
			threeDecimalFormat.setRoundingMode(RoundingMode.HALF_UP);
			rtnDouble = Double.parseDouble(threeDecimalFormat.format(original));
			break;
		case fourDigitDecimal:
			fourDecimalFormat.setRoundingMode(RoundingMode.HALF_UP);
			rtnDouble = Double.parseDouble(fourDecimalFormat.format(original));
			break;
		case fiveDigitDecimal:
			fiveDecimalFormat.setRoundingMode(RoundingMode.HALF_UP);
			rtnDouble = Double.parseDouble(fiveDecimalFormat.format(original));
			break;
		case sixDigitDecimal:
			sixDecimalFormat.setRoundingMode(RoundingMode.HALF_UP);
			rtnDouble = Double.parseDouble(sixDecimalFormat.format(original));
			break;
		default:
			twoDecimalFormat.setRoundingMode(RoundingMode.HALF_UP);
			rtnDouble = Double.parseDouble(twoDecimalFormat.format(original));
			break;
		}

		return rtnDouble;
	}

	public static double formatDigitDecimal(String originalStr, int digit) {
		Double original = Double.parseDouble(originalStr);
		Double rtnDouble;
		switch (digit) {
		case zeroDigitDecimal:
			zeroDecimalFormat.setRoundingMode(RoundingMode.HALF_UP);
			rtnDouble = Double.parseDouble(zeroDecimalFormat.format(original));
			break;
		case oneDigitDecimal:
			oneDecimalFormat.setRoundingMode(RoundingMode.HALF_UP);
			rtnDouble = Double.parseDouble(oneDecimalFormat.format(original));
			break;
		case twoDigitDecimal:
			twoDecimalFormat.setRoundingMode(RoundingMode.HALF_UP);
			rtnDouble = Double.parseDouble(twoDecimalFormat.format(original));
			break;
		case threeDigitDecimal:
			threeDecimalFormat.setRoundingMode(RoundingMode.HALF_UP);
			rtnDouble = Double.parseDouble(threeDecimalFormat.format(original));
			break;
		case fourDigitDecimal:
			fourDecimalFormat.setRoundingMode(RoundingMode.HALF_UP);
			rtnDouble = Double.parseDouble(fourDecimalFormat.format(original));
			break;
		case fiveDigitDecimal:
			fiveDecimalFormat.setRoundingMode(RoundingMode.HALF_UP);
			rtnDouble = Double.parseDouble(fiveDecimalFormat.format(original));
			break;
		case sixDigitDecimal:
			sixDecimalFormat.setRoundingMode(RoundingMode.HALF_UP);
			rtnDouble = Double.parseDouble(sixDecimalFormat.format(original));
			break;
		default:
			twoDecimalFormat.setRoundingMode(RoundingMode.HALF_UP);
			rtnDouble = Double.parseDouble(twoDecimalFormat.format(original));
			break;
		}

		return rtnDouble;
	}

	public static Double ZERO = 0.00d;
	public static Double DEFAILT_DOU = 0.00;//ada 2010-04-19 defaultValue
	private static final int DEFAULT_DIV_SCALE = 10;
	public static NumberFormat usNf = NumberFormat.getInstance(Locale.US);

	public static final int zeroDigitDecimal = 0;
	public static DecimalFormat zeroDecimalFormat = new DecimalFormat("#");
	public static final int oneDigitDecimal = 1;
	public static DecimalFormat oneDecimalFormat = new DecimalFormat("#.#");
	public static final int twoDigitDecimal = 2;
	public static DecimalFormat twoDecimalFormat = new DecimalFormat("#.##");
	public static final int threeDigitDecimal = 3;
	public static DecimalFormat threeDecimalFormat = new DecimalFormat("#.###");
	public static final int fourDigitDecimal = 4;
	public static DecimalFormat fourDecimalFormat = new DecimalFormat("#.####");
	public static final int fiveDigitDecimal = 5;
	public static DecimalFormat fiveDecimalFormat = new DecimalFormat("#.#####");
	public static final int sixDigitDecimal = 6;
	public static DecimalFormat sixDecimalFormat = new DecimalFormat("#.######");
}